<template>
  <div class="role">
    <page-search :searchConfig="formConfig"></page-search>
    <pageContent
      :contentTableConfig="contentTableConfig"
      pageName="Role"
      @edit-btn-click="handelEditData"
      @new-btn-click="handelNewData"
    ></pageContent>
    <page-model
      ref="pageModelRef"
      :default-info="defaultInfo"
      page-name="Role"
      :model-config="modelConfig"
      :other-info="otherInfo"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          :props="{ children: 'children', label: 'name' }"
          show-checkbox
          node-key="id"
          @check="handleCheckChange"
        />
      </div>
    </page-model>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed, ref, nextTick } from 'vue'
import { useStore } from '@/store'
import pageContent from '@/components/page-content'
import { contentTableConfig } from './config/content.config'
import pageSearch from '@/components/page-search'
import formConfig from './config/search'
import pageModel from '@/components/page-model'
import modelConfig from './config/model.config'
import { usePageModel } from '@/hooks/use-page-model'
import { getMenuLeafkeys } from '@/utils/map-menus'
import { ElTree } from 'element-plus'
export default defineComponent({
  name: 'role',
  components: {
    pageContent,
    pageSearch,
    pageModel
  },
  setup() {
    const elTreeRef = ref<InstanceType<typeof ElTree>>()
    const editCallback = (item: any) => {
      const leftKeys = getMenuLeafkeys(item.menuList)
      nextTick(() => {
        elTreeRef.value?.setCheckedKeys(leftKeys, false)
      })
    }
    const { pageModelRef, handelEditData, handelNewData, defaultInfo } =
      usePageModel(undefined, editCallback)
    const store = useStore()
    const menus = computed(() => {
      return store.state.entirMenu
    })

    const otherInfo = ref({})
    const handleCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    return {
      contentTableConfig,
      formConfig,
      modelConfig,
      pageModelRef,
      defaultInfo,
      handelEditData,
      handelNewData,
      menus,
      otherInfo,
      handleCheckChange,
      elTreeRef
    }
  }
})
</script>

<style scoped lang="less">
.menu-tree {
  margin-left: 20px;
}
</style>
